package demo.lock;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-26
 */
public class Main {
    public static void main(String[] args) {
        A t = new A();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }
}

class A extends Thread {
    private boolean loop = true;
    private int count = 20;
    public  void work() {
        synchronized (this) {
            if (count <= 0) {
                System.out.println( "拿完了！！！");
                loop = false;
                return;
            }
            count--;
            System.out.println("子线程" + Thread.currentThread().getName() + "==" + count);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void run() {
        while (loop) {
            work();
        }
    }
}
